# coding=utf-8
# 代码文件：chapter4/ch4.7.py
import sqlite3

dbf = r'data\db\QuoteDB.db'
try:
    con = sqlite3.connect(dbf)      # 1.建立数据库连接
    cursor = con.cursor()           # 2.创建游标对象

    # 按股票代码查询的SQL语句
    sql = r'''SELECT HDate,Open,High,Low,Close,Volume,Symbol 
    FROM  HistoricalQuote 
    WHERE Symbol=?'''
    # 股票代号参数
    paramlist = [600519]
    cursor.execute(sql, paramlist)      # 3. 执行SQL语句

    resultset = cursor.fetchall()       # 4. 提取结果集

    for row in resultset:
        tempstr = '{},{},{},{},{},{},{}'.format(row[0], row[1], row[2], row[3], row[4], row[5], row[6])
        # 打印字段内容
        print(tempstr)

except sqlite3.Error as e:
    print('数据查询发生错误：{}'.format(e))
finally:

    if cursor:
        cursor.close()                  # 5.关闭游标
    if con:
        con.close()                     # 6.关闭数据连接
